using System;
using System.Windows;
using System.Windows.Controls;
namespace MicroMsg.UI.UserContrl
{
	public class NinePatchImg : Panel
	{
		private const string PreCacheStr = "NinePatchImageCache";
		private static bool isPreLoad;
		private static string[] PreLoadImgUrl;
		private NinePatchSrc[] mNinePatchSrc = new NinePatchSrc[9];
		public static DependencyProperty SrcProperty;
		public string Src
		{
			get
			{
				return (string)base.GetValue(NinePatchImg.SrcProperty);
			}
			set
			{
				base.SetValue(NinePatchImg.SrcProperty, value);
			}
		}
		public static void PreLoad()
		{
			if (NinePatchImg.isPreLoad)
			{
				return;
			}
			for (int i = 0; i < NinePatchImg.PreLoadImgUrl.Length; i++)
			{
				NinePatchImg.load(NinePatchImg.PreLoadImgUrl[i]);
			}
			NinePatchImg.isPreLoad = true;
		}
		private static void load(string imgUrl)
		{
			NinePatchHelper ninePatchHelper = new NinePatchHelper();
			NinePatchSrc[] ninePathSrc = ninePatchHelper.GetNinePathSrc(imgUrl);
			NinePatchHelper.SaveNinePatchSrc("NinePatchImageCache" + imgUrl, ninePathSrc);
		}
		public NinePatchImg()
		{
			for (int i = 0; i < 9; i++)
			{
				this.mNinePatchSrc[i] = new NinePatchSrc();
				this.mNinePatchSrc[i].double_0 = 0.0;
				this.mNinePatchSrc[i].double_1 = 0.0;
				this.mNinePatchSrc[i].writeableBitmap_0 = null;
			}
			for (int j = 0; j < 9; j++)
			{
				Image image = new Image();
				image.set_Stretch(1);
				base.get_Children().Add(image);
			}
		}
		private void RefreshLayout()
		{
			if (string.IsNullOrEmpty(this.Src))
			{
				return;
			}
			if (this.mNinePatchSrc[0].writeableBitmap_0 == null)
			{
				return;
			}
			for (int i = 0; i < 9; i++)
			{
				Image image = base.get_Children().get_Item(i) as Image;
				image.set_Source(this.mNinePatchSrc[i].writeableBitmap_0);
			}
		}
		private double GetX(int int_0, double fatherW)
		{
			if (int_0 != 0 && int_0 != 3)
			{
				if (int_0 != 6)
				{
					if (int_0 != 2 && int_0 != 5)
					{
						if (int_0 != 8)
						{
							if (int_0 != 1 && int_0 != 4)
							{
								if (int_0 != 7)
								{
									return 0.0;
								}
							}
							return this.mNinePatchSrc[int_0 - 1].double_0;
						}
					}
					return fatherW - this.mNinePatchSrc[int_0].double_0;
				}
			}
			return 0.0;
		}
		private double GetY(int int_0, double fatherH)
		{
			if (int_0 != 0 && int_0 != 1)
			{
				if (int_0 != 2)
				{
					if (int_0 != 3 && int_0 != 4)
					{
						if (int_0 != 5)
						{
							if (int_0 != 6 && int_0 != 7)
							{
								if (int_0 != 8)
								{
									return 0.0;
								}
							}
							return fatherH - this.mNinePatchSrc[int_0].double_1;
						}
					}
					return this.mNinePatchSrc[int_0 - 3].double_1;
				}
			}
			return 0.0;
		}
		public double GetW(int int_0, double fatherW)
		{
			if (int_0 != 0 && int_0 != 2 && int_0 != 3 && int_0 != 5 && int_0 != 6)
			{
				if (int_0 != 8)
				{
					if (int_0 != 1 && int_0 != 4)
					{
						if (int_0 != 7)
						{
							return 0.0;
						}
					}
					return fatherW - this.mNinePatchSrc[int_0 - 1].double_0 - this.mNinePatchSrc[int_0 + 1].double_0;
				}
			}
			return this.mNinePatchSrc[int_0].double_0;
		}
		public double GetH(int int_0, double fatherH)
		{
			if (int_0 != 0 && int_0 != 1 && int_0 != 2 && int_0 != 6 && int_0 != 7)
			{
				if (int_0 != 8)
				{
					if (int_0 != 3 && int_0 != 4)
					{
						if (int_0 != 5)
						{
							return 0.0;
						}
					}
					return fatherH - this.mNinePatchSrc[int_0 - 3].double_1 - this.mNinePatchSrc[int_0 + 3].double_1;
				}
			}
			return this.mNinePatchSrc[int_0].double_1;
		}
		private double getMinW()
		{
			double num = this.mNinePatchSrc[0].double_0 + this.mNinePatchSrc[1].double_0 + this.mNinePatchSrc[2].double_0;
			double num2 = this.mNinePatchSrc[3].double_0 + this.mNinePatchSrc[4].double_0 + this.mNinePatchSrc[5].double_0;
			double num3 = this.mNinePatchSrc[6].double_0 + this.mNinePatchSrc[7].double_0 + this.mNinePatchSrc[8].double_0;
			return Math.Max(num, Math.Max(num2, num3));
		}
		private double getMinH()
		{
			double num = this.mNinePatchSrc[0].double_1 + this.mNinePatchSrc[3].double_1 + this.mNinePatchSrc[6].double_1;
			double num2 = this.mNinePatchSrc[1].double_1 + this.mNinePatchSrc[4].double_1 + this.mNinePatchSrc[7].double_1;
			double num3 = this.mNinePatchSrc[2].double_1 + this.mNinePatchSrc[5].double_1 + this.mNinePatchSrc[8].double_1;
			return Math.Max(num, Math.Max(num2, num3));
		}
		protected override Size MeasureOverride(Size availableSize)
		{
			if (base.get_Children().get_Count() <= 0)
			{
				return base.MeasureOverride(availableSize);
			}
			double minW = this.getMinW();
			double minH = this.getMinH();
			bool flag = false;
			if (availableSize.get_Width() < minW)
			{
				flag = true;
				availableSize.set_Width(minW);
			}
			if (availableSize.get_Height() < minH)
			{
				flag = true;
				availableSize.set_Height(minH);
			}
			if (flag)
			{
				return availableSize;
			}
			Size result = base.MeasureOverride(availableSize);
			if (double.IsInfinity(availableSize.get_Height()))
			{
				return result;
			}
			if (double.IsInfinity(availableSize.get_Width()))
			{
				return result;
			}
			return availableSize;
		}
		protected override Size ArrangeOverride(Size finalSize)
		{
			if (base.get_Children().get_Count() <= 0)
			{
				return base.ArrangeOverride(finalSize);
			}
			double minW = this.getMinW();
			double minH = this.getMinH();
			if (finalSize.get_Width() < minW)
			{
				finalSize.set_Width(minW);
			}
			if (finalSize.get_Height() < minH)
			{
				finalSize.set_Height(minH);
			}
			for (int i = 0; i < base.get_Children().get_Count(); i++)
			{
				double x = this.GetX(i, finalSize.get_Width());
				double y = this.GetY(i, finalSize.get_Height());
				double w = this.GetW(i, finalSize.get_Width());
				double h = this.GetH(i, finalSize.get_Height());
				base.get_Children().get_Item(i).Arrange(new Rect(x, y, w, h));
			}
			return finalSize;
		}
		static NinePatchImg()
		{
			NinePatchImg.isPreLoad = false;
			NinePatchImg.PreLoadImgUrl = new string[]
			{
				"Resource/Img/Chat/chatfrom_bg_normal.9.png",
				"Resource/Img/Chat/chatto_bg_normal.9.png",
				"Resource/Img/Chat/left_voice_normal.9.png",
				"Resource/Img/Chat/right_voice_normal.9.png"
			};
			NinePatchImg.SrcProperty = DependencyProperty.Register("Src", typeof(string), typeof(NinePatchImg), new PropertyMetadata("", new PropertyChangedCallback(NinePatchImg.OnItemPropertyChanged)));
		}
		private static void OnItemPropertyChanged(DependencyObject dependencyObject_0, DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (dependencyPropertyChangedEventArgs_0.get_Property() == NinePatchImg.SrcProperty)
			{
				(dependencyObject_0 as NinePatchImg).OnSrcPropertyChanged(dependencyPropertyChangedEventArgs_0);
			}
		}
		private void OnSrcPropertyChanged(DependencyPropertyChangedEventArgs dependencyPropertyChangedEventArgs_0)
		{
			if (string.IsNullOrEmpty(this.Src))
			{
				return;
			}
			NinePatchSrc[] ninePatchSrc = NinePatchHelper.GetNinePatchSrc("NinePatchImageCache" + this.Src);
			if (ninePatchSrc == null)
			{
				NinePatchHelper ninePatchHelper = new NinePatchHelper();
				this.mNinePatchSrc = ninePatchHelper.GetNinePathSrc(dependencyPropertyChangedEventArgs_0.get_NewValue() as string);
				NinePatchHelper.SaveNinePatchSrc("NinePatchImageCache" + this.Src, this.mNinePatchSrc);
			}
			else
			{
				this.mNinePatchSrc = ninePatchSrc;
			}
			this.RefreshLayout();
		}
	}
}
